#!/bin/bash
# export a dir from HG and build an RPM from it
VERSION='$Id: hg2rpm 6927 2012-03-14 13:40:00Z fbusse $'
if ! test "$1" ; then
        cat <<EOF
$0 $VERSION
Export from HG and build an RPM package.
SPEC file must be part of files checked out from HG

Usage:

$(basename "$0") <WORKING COPY PATH> [more rpmbuild parameters ...]

Example:
$(basename "$0") ~/src/intern/perl_mod/trunk
$(basename "$0") ~/src/intern/perl_mod/trunk --define="key value"


This will checkout the URL to perl_mod-0.0.16 and create a tar.gz archive
from it with the name of perl_mod-0.0.16.tar.gz and call rpmbuild -ta on 
the resulting tar and remove the tar and checked out directory if the
RPM building was successful.

The current working directory is used as a work space for temporary files.

Note: The RPM package name and version are taken from the SPEC file!!

Author: Fridtjof Busse (based on svn2rpm by Schlomo Schapiro, Stefan Schubert)

EOF
exit 10
fi

function die {
        echo "ERROR: $@"
        KEEPWORKDIR=1
        exit 1
}

function exittrap {
        if test "$KEEPWORKDIR" -a -s "$WORK/log.txt" ; then
                sed -e 's/^/LOG: /' "$WORK/log.txt"
        fi
        ${KEEPWORKDIR:+echo Do not forget to} rm -Rf "$WORK"
}

HG="$1" ; shift
export TMPDIR=${TMPDIR:-/data/tmp}
WORK="$(mktemp -d -t "$(basename $0)"-XXXXXXXXXX)"
# KEEPWORKDIR can be set externally to keep the work dir. Else it is empty by default.
trap "exittrap" 0

# get HG rev for URL and remove whitespaces
REV="$(hg log -l1 "$HG" | grep changeset | cut -d: -f2 | tr -d ' ')" >> "$WORK"/log.txt 2>&1 || die "Could not determine Mercurial revision"
# make sure there is a REV
if ! [ "$REV" ] ; then
        REV=0
fi


cp -r "$HG" "$WORK/export" >> "$WORK"/log.txt 2>&1 || die "HG export failed"

# find spec files in content
SPECFILES=( $(find "$WORK/export" -maxdepth 1 -type f -name \*.spec) ) >> "$WORK"/log.txt 2>&1 || die "Could not search for spec files"
if test "${#SPECFILES[@]}" -ne 1 ; then
        # make sure that there is exactly 1 spec file in the tar.gz. Everything else (0 or more than 1) is an error.
        die "There is not exactly 1 spec file (${SPECFILES[@]})"
fi

# patch rev into spec file
sed -i -e "s/^\(Release: .*\)$/\1.$REV/" "$SPECFILES" || die "Could not patch REV into '$SPECFILES'"

# get package name and version from spec file
PACKAGE_NAME="$( sed -ne '/Name:/{s/.*: \(.*\)/\1/;P}' "$SPECFILES" )"
PACKAGE_VERSION="$( sed -ne '/Version:/{s/.*: \(.*\)/\1/;P}' "$SPECFILES" )"
PACKAGE="$PACKAGE_NAME-$PACKAGE_VERSION"
test "$PACKAGE" = "-" && die "Could not determine correct package name and version from SPEC file (got $PACKAGE)"

# move export dir to correct package name-version
mv -v "$WORK/export" "$WORK/$PACKAGE" >> "$WORK"/log.txt 2>&1 || die "Could not move '$WORK/export' to '$WORK/$PACKAGE'"

# prepare rpm build environment
mkdir -p "$WORK"/rpmbuild/{SPECS,SOURCES,BUILD,RPMS/noarch,RPMS/x86_64,RPMS/i386,RPMS/i486,RPMS/i586,RPMS/i686,SRPMS}

# pack tgz and build RPMs
tar -cvzf "$WORK/$PACKAGE".tar.gz -C "$WORK" "$PACKAGE" >> "$WORK"/log.txt 2>&1 || die "Could not pack archive"
rpmbuild --define="_topdir $WORK/rpmbuild" "$@" -ta "$WORK/$PACKAGE".tar.gz >> "$WORK/log.txt" 2>&1 || die "Could not build RPM"

# collect resulting RPMs of rpmbuild
RESULT_FILES=()
for f in $WORK/rpmbuild/RPMS/*/*rpm $WORK/rpmbuild/SRPMS/*rpm ; do
        RESULT_FILES=( "${RESULT_FILES[@]}" "$(basename "$f")" )
        mv "$f" . || die "Could not move '$f' to '$REPOTARGET/$arch'"
done

# handle the case if there is no result. Should be handled by the || die above but just in case ...
if test "$RESULT_FILES" ; then
        echo "Created the following RPM files:"
        for f in "${RESULT_FILES[@]}" ; do
                echo "  $f"
        done
else
        die "No RPMs were created"
fi
